package com.basiclib.utils

import android.app.Activity
import android.app.Fragment
import android.content.Context
import android.content.SharedPreferences

class PreferencesMgr private constructor(val name: String, val mode: Int) {
    object Holder{
        val instance = PreferencesMgr("sp_default",Context.MODE_PRIVATE)
    }
    companion object {
        fun instance():PreferencesMgr{
            return Holder.instance
        }
    }


}


inline fun Context.spHelper(): SharedPreferences =
    getSharedPreferences(PreferencesMgr.instance().name, PreferencesMgr.instance().mode)

inline fun Activity.spHelper(): SharedPreferences =
    getSharedPreferences(PreferencesMgr.instance().name, PreferencesMgr.instance().mode)

inline fun Fragment.spHelper(): SharedPreferences =
    activity.getSharedPreferences(PreferencesMgr.instance().name, PreferencesMgr.instance().mode)

inline fun SharedPreferences.put(key: String, value: Int): Boolean = this.edit().putInt(key, value).commit()
inline fun SharedPreferences.put(key: String, value: Long): Boolean = this.edit().putLong(key, value).commit()
inline fun SharedPreferences.put(key: String, value: Float): Boolean = this.edit().putFloat(key, value).commit()
inline fun SharedPreferences.put(key: String, value: String): Boolean = this.edit().putString(key, value).commit()
inline fun SharedPreferences.put(key: String, value: Boolean): Boolean = this.edit().putBoolean(key, value).commit()
inline fun SharedPreferences.putSet(key: String, value: MutableSet<String>): Boolean =
    this.edit().putStringSet(key, value).commit()

inline fun SharedPreferences.get(key: String, defaultValue: Int): Int = this.getInt(key, defaultValue)
inline fun SharedPreferences.get(key: String, defaultValue: Float): Float = this.getFloat(key, defaultValue)
inline fun SharedPreferences.get(key: String, defaultValue: Long): Long = this.getLong(key, defaultValue)
inline fun SharedPreferences.get(key: String, defaultValue: Boolean): Boolean = this.getBoolean(key, defaultValue)
inline fun SharedPreferences.get(key: String, defaultValue: String): String = this.getString(key, defaultValue)
inline fun SharedPreferences.get(key: String, defaultValue: MutableSet<String>): MutableSet<String> =
    this.getStringSet(key, defaultValue)

inline fun SharedPreferences.remove(key: String): Boolean = this.edit().remove(key).commit()
